package javautils;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class dateTimeUtils {
	public static void main(String args[]) {
		System.out.println(getCurrentTime());
	}

	/**
	 * 获取系统当前时间(格式:yyyyMMddHHmm)
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		// SimpleDateFormat df = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		// SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");// 设置日期格式
		// return df.format(new Date());// new Date()为获取当前系统时间
		return MessageFormat.format("{0,date,yyyy-MM-dd-HH-mm:ss}",
				new Object[] { new java.sql.Date(System.currentTimeMillis()) });
	}

	static String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
			"星期六" };

	/**
	 * 字符串转换成星期
	 * 
	 * @param str
	 * @return date
	 */
	public static String getWeekOfDate(String str) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (weekday < 0) {
			weekday = 0;
		}
		return weekDays[weekday];
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param str
	 * @return yyyy年MM月dd日
	 */
	public static String formateDate(String str) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return str;
		}
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日");
		String strDate = format2.format(date);
		return strDate;
	}
}
